package com.tywj.panda.travel.details

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.Gravity
import android.view.WindowManager
import androidx.fragment.app.FragmentManager
import com.tywj.panda.customer.common.view.dialog.BottomDialog
import com.tywj.panda.travel.R
import com.tywj.lib.core.common.extension.onClick
import com.tywj.lib.core.common.util.ResourcesUtil
import com.tywj.panda.customer.common.entity.TravelEntity
import com.tywj.panda.customer.common.tool.MoneyTool
import com.tywj.panda.travel.tool.ServiceFeeTool
import kotlinx.android.synthetic.main.travel_refund_dialog.*


/**
 * 退款对话框
 */
class TravelRefundDialog : BottomDialog() {

    companion object {
        const val TAG = "TravelRefundDialog"
    }

    //默认的票数
    private var num = 1

    //行程中返回的车票数
    private var ticketsNum = 1

    override fun getLayoutID(): Int = R.layout.travel_refund_dialog

    private lateinit var listener: (number: Int, bean: TravelEntity.DataBean?) -> Unit

    private var ticketTime = ""

    //退款金额
    private var refundFee = 0

    //手续费
    private var serviceFee = 0


    private var beanDetails: TravelEntity.DataBean? = null

    override fun getGravity(): Int = Gravity.CENTER

    override fun getWidth(): Int = WindowManager.LayoutParams.WRAP_CONTENT

    @SuppressLint("SetTextI18n")
    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        beanDetails?.let {

            ticketsNum = if (it.number == 0) {
                1
            } else {
                it.number
            }

            ticketTime = it.lineDate + " " + it.onTime

            tvTime.text = ticketTime
            tvLineName.text = it.lineName
        }
        refreshFee(num)
    }

    //This is called after onActivityCreated(Bundle) and before onStart()}.
    override fun onViewStateRestored(savedInstanceState: Bundle?) {
        super.onViewStateRestored(savedInstanceState)

        //清空缓存的内容
        num = 1
        tvNum.text = num.toString()
        //也可以自定义组件并重写getFreezesText() 返回false。
    }

    override fun initListener() {
        super.initListener()

        ivClose.onClick {
            dismiss()
        }

        tvCancel.onClick {
            dismiss()
        }
        tvConfirm.onClick {
            beanDetails?.let { beanDetails ->

                //回调给服务器
                val refundEntity = TravelEntity.DataBean()

                refundEntity.ticketCode = beanDetails.ticketCode
                //退款金额
                refundEntity.refundFee = refundFee
                //手续费
                refundEntity.chargeService = serviceFee

                listener.invoke(num, refundEntity)
            }
            dismiss()
        }

        ibPlus.setOnClickListener {
            num--

            if (num <= 0) {
                num = 1
                shortToast("最小数为1")
            }
            refreshFee(num)
        }
        ibAdd.setOnClickListener {
            num++

            if (num > ticketsNum) {
                num = ticketsNum
                shortToast("不能超过你购买的最大车票数：$ticketsNum")
            }
            refreshFee(num)
        }
    }

    /**
     * 显示弹窗
     */
    fun showRefundDialog(
        fragmentManager: FragmentManager,
        bean: TravelEntity.DataBean?
    ) {
        this.beanDetails = bean
        showDialog(fragmentManager)
    }


    fun setConfirmOnClick(listener: (number: Int, bean: TravelEntity.DataBean?) -> Unit) {
        this.listener = listener
    }

    private fun refreshFee(num: Int) {
        tvNum.text = num.toString()

        //退款金额
        refundFee = ServiceFeeTool.getFee(beanDetails!!.price * num, ticketTime)

        //手续费
        serviceFee = beanDetails!!.price * num - refundFee


        tvRefundMoney.text =
            String.format(ResourcesUtil.getString(R.string.travel_refund_fee), MoneyTool.formatFen(refundFee))


        tvServiceCharge.text =
            String.format(ResourcesUtil.getString(R.string.travel_service_fee), MoneyTool.formatFen(serviceFee))
    }
}